{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas # 读取csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建一个数据集\n",
    "import os\n",
    "os.makedirs(os.path.join(\".\",\"data\",), exist_ok=True)\n",
    "data_file = os.path.join(\".\",\"data\",\"house_tiny.csv\")\n",
    "if not os.path.isfile(data_file):\n",
    "    with open(data_file,\"w\") as f:\n",
    "        f.write('NumRooms,Alley,Price\\n')  # 列名\n",
    "        f.write('NA,Pave,127500\\n')  # 每行表示一个数据样本\n",
    "        f.write('2,NA,106000\\n')\n",
    "        f.write('4,NA,178100\\n')\n",
    "        f.write('NA,NA,140000\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取数据\n",
    "data = pandas.read_csv(\"./data/house_tiny.csv\")\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 分为输入输出\n",
    "inputs, outputs = data.iloc[:,:2], data.iloc[:,2]\n",
    "print(inputs)\n",
    "print(outputs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 处理数值空值，此处用平均值填充\n",
    "bef = id(inputs)\n",
    "inputs[:] = inputs.fillna(inputs.mean())\n",
    "inputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 处理离散的类型空值\n",
    "inputs = pandas.get_dummies(inputs, dummy_na=True)\n",
    "print(inputs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转化为张量格式\n",
    "import torch\n",
    "x = torch.tensor(inputs.to_numpy(dtype=float))\n",
    "y = torch.tensor(outputs.to_numpy(dtype=float))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
